package com.zcah;

import com.zcah.dao.RedisUtil;
import com.zcah.repository.ArticleService;
import com.zcah.spider.WechatArticleSpider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.ArrayList;
import java.util.List;

public class AppMain {


    public static void main(String[] args) throws Exception {
        Logger logger = LoggerFactory.getLogger(AppMain.class);
        AnnotationConfigApplicationContext ctx =
                new AnnotationConfigApplicationContext();
        ctx.scan("com.zcah");
        ctx.refresh();
        ArticleService articleService = ctx.getBean(ArticleService.class);
        RedisUtil redisUtil = ctx.getBean(RedisUtil.class);
        List<String> list = new ArrayList<String>();

        while (true) {
            Thread.sleep(5000);
            if (redisUtil.len("url_list") > 100) {
                for (int i = 0; i < 100; i++) {
                    String url = redisUtil.popLink("url_list");
                    if (url != null && url.startsWith("http")) {
                        list.add(url);
                        if (list.size() == 100) {
                            logger.info("spider is running");
                            new WechatArticleSpider( articleService, redisUtil, "wechatspider", true, list);
                            list.clear();
                        }
                    }
                }
            }
        }
    }
}
